Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onDispose Functionality and Modified deps Type to Array in useEffect #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

psyrenpark
Copy link

Hello ~,

I needed an onDispose functionality within useEffect, so I've added it. Additionally, I have changed the type of deps to an array.

I would appreciate it if you could review them.

// example

    /*
    useEffect(()=>{
        console.log("Call once")
    },[])
    */
    useEffect(onEffect = {
        println("Call once")
    })

    /*
    useEffect(()=>{
        console.log("Call once")
        return  () => {
            console.log("onDispose")
        }
    },[])
    */
    useEffect(onEffect = {
            println("Call once")
        }, onDispose = {
            println("onDispose")
        }
    )

    /*
    useEffect(()=>{
        console.log("Call when the value of isOn changes")
        return  () => {
            console.log("onDispose")
        }
    },[isOn, "SOME" ])
    */
    useEffect(arrayOf(isOn,"SOME"),
        onEffect =  {
            println("Call when the value of isOn changes  [isOn] $isOn")
        }, onDispose = {
            println("onDispose")
        }
    )

Hello,

I have made some modifications while using it.

I would appreciate it if you could review them.
@junerver
Copy link

junerver commented Mar 8, 2024

In fact, every timeif you pass deps through arrayOf, a new array instance will be created, which will cause LaunchedEffect and DisposeEffect to execute blocks. If you just want to execute when the component is loaded or when it ends, you can declare a custom hook. For example: useMount, useUnmount;

Of course, if you insist on doing this through a hook, then your deps should be vararg deps:Any, and use *deps when passing it to XXEffect so that it can be tracked correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants